//
//  ShopBagTableViewFootCell.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/17.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ShopBagTableViewFootCell.h"
#import "CLZWebviewViewController.h"
#import "ShopBagViewController.h"
#import "LocalStateManager.h"
@interface ShopBagTableViewFootCell()

@property (weak, nonatomic) IBOutlet UILabel *sumTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *sumPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *taxLabel;
@property (weak, nonatomic) IBOutlet UIView *spaceLine;
@property (weak, nonatomic) IBOutlet UILabel *taxTipLabel;
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property (nonatomic, assign) CGFloat leftWidth;
@property (nonatomic, assign) CGFloat rightWidht;
@property (nonatomic, assign) CGFloat viewHeight;
@property (weak, nonatomic) IBOutlet UIImageView *taxIcon;
@property (nonatomic, strong) NSArray *totalArray;
@property (weak, nonatomic) IBOutlet UIView *bottomLine;
@property (weak, nonatomic) IBOutlet UIView *bgView;
- (IBAction)selectAction:(id)sender;
- (IBAction)weituoAction:(id)sender;
- (IBAction)yonghuxieyiAction:(id)sender;




@end
@implementation ShopBagTableViewFootCell

- (void)awakeFromNib {
    // Initialization code
    self.taxLabel.userInteractionEnabled = YES;
    [self.taxLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taxAction:)]];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"行邮税用户声明"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    self.taxLabel.attributedText = content;
    self.selectBt.selected=[LocalStateManager shareLocalStateManager].selected;
    
}

- (void)updateWithSettleInfo:(NSArray *)array {
    [self removeSubViews:self.leftView];
    self.totalArray = array;
    [self removeSubViews:self.rightView];
    CGFloat originY = 10.f;
    if (array.count > 1) {
        for (int index = 0; index < array.count - 1; index ++) {
            CartTotalModel *model = [array objectAtIndex:index];
            UILabel *leftLabel = [self labelWithText:model.title];
            CGFloat leftWidth = [self stringWidth:model.title];
            [self.leftView addSubview:leftLabel];
            self.leftWidth = self.leftWidth < leftWidth ? leftWidth : self.leftWidth;
            leftLabel.frame = CGRectMake(0, originY, self.leftWidth, 12.f);

            UILabel *rightLabel = [self labelWithText:model.text];
            CGFloat rightWidth = [self stringWidth:model.text];
            [self.rightView addSubview:rightLabel];
            self.rightWidht = self.rightWidht < rightWidth ? rightWidth : self.rightWidht;
            rightLabel.frame = CGRectMake(0, originY, self.rightWidht, 12.f);
            originY += (12 + 10);
        }
        self.viewHeight = originY;
        CartTotalModel *model = [array lastObject];
        self.sumTitleLabel.text = model.title;
        [self.sumTitleLabel sizeToFit];
        self.sumPriceLabel.text = model.text;
        [self.sumPriceLabel sizeToFit];
    }
    [self setNeedsLayout];
}

- (UILabel *)labelWithText:(NSString *)text {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textColor = [UIColor colorWithRGB:0x8b8b8b];
    label.font = [UIFont systemFontOfSize:12.f];
    label.textAlignment = NSTextAlignmentRight;
    return label;
}

- (void)removeSubViews:(UIView *)view {
    if(view.subviews.count > 0) {
        for (UIView *subView in view.subviews) {
            [subView removeFromSuperview];
        }
    }
}

- (CGFloat)stringWidth:(NSString *)text {
    if (text.length > 0) {
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.f] maxSize:CGSizeMake(150.f, 12.f)];
        return size.width;
    }
    return 0;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat sWidth = [[UIScreen mainScreen] bounds].size.width;
    self.rightView.frame = CGRectMake(sWidth - 10 - self.rightWidht, 0, self.rightWidht, self.viewHeight);
    self.leftView.frame = CGRectMake(sWidth - 10 - self.rightWidht - self.leftWidth - 15, 0, self.leftWidth, self.viewHeight);
    self.sumPriceLabel.origin = CGPointMake(sWidth - 10 - self.sumPriceLabel.width, self.viewHeight);
    self.sumTitleLabel.origin = CGPointMake(self.sumPriceLabel.left - 17.f - self.sumTitleLabel.width, self.viewHeight);
    self.spaceLine.top = self.sumPriceLabel.bottom + 12.f;
    self.taxTipLabel.top = self.spaceLine.bottom + 15.f;
    self.bgView.top = self.spaceLine.bottom + 15.f;
    self.bottomLine.top = self.spaceLine.bottom + 44.f;
    self.bgView.right=SCREEN_WIDTH-14;
    self.spaceLine.width=SCREEN_WIDTH-28;
    self.spaceLine.left=14;
   
   
}

- (CGFloat)heightForRow {
    return self.totalArray.count * 12 + 10 * self.totalArray.count + 20 + 45.f;
}

- (void)taxAction:(id)sender {
    UIViewController *vc = [UIView viewController:self];
    CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
    webVC.webTitle=@"常见问题";
    webVC.webUrl=[NSString stringWithFormat:@"%@%@",DOMAIN_ADDRESS,URL_XINGYOUSHUI];
    [vc.navigationController pushViewController:webVC animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)selectAction:(id)sender {
    UIButton *bt=(UIButton*)sender;
    bt.selected=!bt.selected;
    [LocalStateManager shareLocalStateManager].selected=bt.selected;
}

- (IBAction)weituoAction:(id)sender {
    //个人委托
    CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
    webVC.webUrl=[NSString stringWithFormat:@"%@%@",DOMAIN_ADDRESS,URL_entrust];
    UIViewController *currentVC=[UIView viewController:self];
    [currentVC.navigationController pushViewController:webVC animated:YES];

    
}

- (IBAction)yonghuxieyiAction:(id)sender {
    //用户协议
    CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
    webVC.webUrl=[NSString stringWithFormat:@"%@%@",DOMAIN_ADDRESS,URL_entrust];
    UIViewController *currentVC=[UIView viewController:self];
    [currentVC.navigationController pushViewController:webVC animated:YES];
}
@end
